package cn.mumzone.pos.presenter;

import android.util.Log;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.event.DateChangedEvent;
import cn.mumzone.pos.event.SearchBillEvent;
import cn.mumzone.pos.event.SearchInventoryEvent;
import cn.mumzone.pos.model.InventoryItemEntity;
import cn.mumzone.pos.model.source.StockRepository;
import cn.mumzone.pos.presenter.contract.InventoryItemContract;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created By Dlx
 * on 2019/9/25
 */
public class InventoryItemPresenter extends AbsPresenter<InventoryItemContract.View, StockRepository> implements InventoryItemContract.Presenter {
    @Override
    public void initEvent() {
        Subscription subscription = RxBus.getDefault()
                .toObservable(SearchInventoryEvent.class)
                .subscribe(new Action1<SearchInventoryEvent>() {
                    @Override
                    public void call(SearchInventoryEvent searchBillEvent) {
                        mView.onSearchKeyCome(searchBillEvent.getKey());
                    }
                });
        addSubscrebe(subscription);

        Subscription subscription1 = RxBus.getDefault()
                .toObservable(DateChangedEvent.class)
                .subscribe(new Action1<DateChangedEvent>() {
                    @Override
                    public void call(DateChangedEvent searchBillEvent) {
                        if (searchBillEvent.getType() == DateChangedEvent.TYPE_INVENTORY)
                            mView.onDateChanged(searchBillEvent.getTime());
                    }
                });
        addSubscrebe(subscription1);
    }

    @Override
    public void getDatas(final int pageNum, String inventoryType, String inventoryStatus, String startTime, String endTime, String keyword) {
        mView.showLoadingDialog();
        ApiCallBack<PageListResponse<InventoryItemEntity>> callBack = new ApiCallBack<PageListResponse<InventoryItemEntity>>() {
            @Override
            public void onSuccess(PageListResponse<InventoryItemEntity> response) {
                if (response.getList() == null || response.getList().size() == 0) {
                    if (1 == pageNum) {
                        mView.noData();
                    } else {
                        mView.noMore();
                    }
                } else if (response.getList().size() < AppConstants.PAGE_SIZE) {
                    mView.onDataReceived(response);
                    mView.noMore();
                }else {
                    mView.onDataReceived(response);
                }
                mView.dismissLoadingDialog();
            }

            @Override
            public void onNetworkError(BaseResponse response) {
                mView.showNetError();
                mView.dismissLoadingDialog();
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
                mView.dismissLoadingDialog();
            }
        };
        mRepo.getInventoryList(callBack, AppConstants.PAGE_SIZE + "", pageNum + "", keyword, startTime, endTime, inventoryType, inventoryStatus);
        addSubscrebe(callBack);
    }
}
